package com.hanoi.wiki.manager.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.hanoi.wiki.dao.CategoryDAO;
import com.hanoi.wiki.dao.LogDAO;
import com.hanoi.wiki.dao.PlacesDAO;
import com.hanoi.wiki.dao.RatesDAO;
import com.hanoi.wiki.exception.DataAccessException;
import com.hanoi.wiki.manager.PlacesManager;
import com.hanoi.wiki.model.Category;
import com.hanoi.wiki.model.Log;
import com.hanoi.wiki.model.Places;
import com.hanoi.wiki.model.Rates;
import com.hanoi.wiki.model.json.PlacesJson;

@Component
@Transactional
public class PlacesManagerImpl extends BaseManagerImpl implements PlacesManager{

	@Autowired
	private PlacesDAO placesDAO;
	
	@Autowired
	private RatesDAO ratesDAO;
	
	@Autowired
	private LogDAO logDAO;
	
	@Autowired
	private CategoryDAO categoryDAO;
	
	public int save(PlacesJson json) {
		try {
			Places places = new Places(json);
			
			List<Log> logs = new ArrayList<Log>();
			Log log;
			for(Integer i : json.getLogs()) {
				log = logDAO.get(i);
				if(log != null) {
					logs.add(log);
				}
			}
			
			List<Category> categories = new ArrayList<Category>();
			Category category;
			for(Integer i : json.getCategories()) {
				category = categoryDAO.get(i);
				if(category != null) {
					categories.add(category);
				}
			}
			
			Rates rate = ratesDAO.get(json.getRate());
			if(rate != null) {
				places.setRate(rate);
			}
			places.setCategories(categories);
			places.setLogs(logs);
			
			return placesDAO.savePlaces(places);
		} catch (DataAccessException e) {
			log.error("", e);
		}
		return 0;
	}

	public void update(PlacesJson json) {
		try {
			Places places = new Places(json);
			
			List<Log> logs = new ArrayList<Log>();
			Log log;
			for(Integer i : json.getLogs()) {
				log = logDAO.get(i);
				if(log != null) {
					logs.add(log);
				}
			}
			
			List<Category> categories = new ArrayList<Category>();
			Category category;
			for(Integer i : json.getCategories()) {
				category = categoryDAO.get(i);
				if(category != null) {
					categories.add(category);
				}
			}
			
			Rates rate = ratesDAO.get(json.getRate());
			if(rate != null) {
				places.setRate(rate);
			}
			places.setCategories(categories);
			places.setLogs(logs);
			places.setId(json.getId());
			
			placesDAO.update(places);
		} catch (DataAccessException e) {
			log.error("", e);
		}
	}

	public PlacesJson get(Integer id) {
		try {
			return new PlacesJson(placesDAO.get(id));
		} catch (DataAccessException e) {
			log.error("", e);
		}
		return null;
	}

	public List<PlacesJson> getList() {
		List<PlacesJson> placesJsonList = new ArrayList<PlacesJson>();
		List<Places> placesList = placesDAO.getList();
		try {
			for(Places places : placesList) {
				placesJsonList.add(new PlacesJson(places));
			}
		} catch (DataAccessException e) {
			log.error("", e);
		}
		return placesJsonList;
	}

	public void remove(Integer id) {
		try {
			if(placesDAO.get(id) != null) {
				placesDAO.remove(id);
			}
		} catch (DataAccessException e) {
			log.error("", e);
		}
	}

}
